package com.ruoyi.common.utils.Date;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * AUTHOR : CREATED BY DALE MAO ON 2018/9/24
 * DESCRIPTION:
 * Time:2018/9/24 ----- ${Time}
 **/
public class DateFormat {

    public final static String YYYY_MM_DD = "yyyy-MM-dd";
    public final static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    /**
     * 获取一天的开始时间和结束时间
     * @return
     */
    public static Date getStartTime() {

        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    /**
     * 获取查询当天的结束时间时间
     * @return
     */
    public static Date getnowEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime();
    }


    /*
  * 将时间转换为时间戳
  */
    public static String dateToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }




    /*
    * 将时间戳转换为时间
    */
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /*
    * 将当天开始时间转换为时间戳
    */
    public static String startDateToStamp() throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        String s =simpleDateFormat.format(todayStart.getTime());
        System.out.println(s);
        String res;
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime()/1000;
        res = String.valueOf(ts);
        System.out.println(res);
        return res;
        //return  DateFormat.dateToStamp(s);
    }

    /*
     * 将当天最后时间转换为时间戳
     */
    public static String endDateToStamp() throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        String s =simpleDateFormat.format(todayEnd.getTime());
        System.out.println(s);
        String res;
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime()/1000;
        res = String.valueOf(ts);
        System.out.println(res);
        return res;
    }





    /**
     * 获取查询某日的结束时间
     * @return
     */
    public static String getStartTimeByTime(Timestamp time) {
        SimpleDateFormat SDF = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        Date date = new Date(time.getTime());
        Calendar Start= Calendar.getInstance();
        Start.setTime(date);
        Start.set(Calendar.HOUR_OF_DAY,0);
        Start.set(Calendar.MINUTE,0);
        Start.set(Calendar.SECOND,0);
        Start.set(Calendar.MILLISECOND, 0);
        String s = SDF.format(Start.getTime());
        return s;
    }
    /**
     * 获取查询某日的结束时间
     * @return
     */
    public static String getnextTimeByTime(Timestamp time) {
        SimpleDateFormat SDF = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        Date date = new Date(time.getTime());
        Calendar Start= Calendar.getInstance();
        Start.setTime(date);
        Start.set(Calendar.HOUR_OF_DAY,Start.get(Calendar.HOUR_OF_DAY)+1);
        Start.set(Calendar.MINUTE,0);
        Start.set(Calendar.SECOND,0);
        Start.set(Calendar.MILLISECOND, 0);
        String s = SDF.format(Start.getTime());
        return s;
    }

    /**
     * 获取查询月的开始时间 当月的开始时间
     * @return
     */
    public static String getFirstDayForMonth(Timestamp time) {
        SimpleDateFormat SDF = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        System.out.println(time);
        Date date = new Date(time.getTime());
        Calendar Start= Calendar.getInstance();
        Start.setTime(date);
        Start.set(Calendar.DAY_OF_MONTH,1);
        Start.set(Calendar.HOUR_OF_DAY,0);
        Start.set(Calendar.MINUTE,0);
        Start.set(Calendar.SECOND,0);
        Start.set(Calendar.MILLISECOND, 0);
        String s = SDF.format(Start.getTime());
        return s;
    }

    /**
     * 获取账单查询月的结束时间  //下个月的1号初始时间
     * @return
     */
    public static String getEndDayForMonth(Timestamp time) {
        SimpleDateFormat SDF = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        System.out.println(time);
        Date date = new Date(time.getTime());
        Calendar Start= Calendar.getInstance();
        Start.setTime(date);
        System.out.println(Calendar.MONTH);
        Start.set(Calendar.MONTH,Start.get(Calendar.MONTH)+1);
        Start.set(Calendar.DAY_OF_MONTH,1);
        Start.set(Calendar.HOUR_OF_DAY,0);
        Start.set(Calendar.MINUTE,0);
        Start.set(Calendar.SECOND,0);
        Start.set(Calendar.MILLISECOND, 0);
        String s = SDF.format(Start.getTime());
        return s;
    }


    /**
     * 获取账单查询月的结束时间  //下个月的1号初始时间
     * @return
     */
    public static Long getLastInfoBYThreeZ(Timestamp time) {

        Long s =time.getTime();
        Long dayChange =1000L*60*60*24*30;
        Long e=s-dayChange;
        return e;
    }
    public static void main(String[] args) throws ParseException {
        String format = "yyyy:MM:DD HH:mm:ss";
        Date nowTime = new SimpleDateFormat(format).parse("2017:8:8 8:27:11");
        Timestamp timestamp =new Timestamp(nowTime.getTime());
        Date startTime = new SimpleDateFormat(format).parse("2017:8:8 09:27:00");
        Timestamp timestamp2 =new Timestamp(startTime.getTime());
        Date endTime = new SimpleDateFormat(format).parse("2017:8:8 09:27:59");
        Timestamp timestamp3 =new Timestamp(endTime.getTime());

        System.out.println(isEffectiveDate(timestamp, timestamp2, timestamp3));



    }

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime 当前时间
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     * @author jqlin
     */
    public static boolean isEffectiveDate(Timestamp nowTime, Timestamp startTime, Timestamp endTime) {
        System.out.println(nowTime.getTime());
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime 当前时间
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     * @author jqlin
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        System.out.println(nowTime.getTime());
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }


    public static boolean checkRedPaperTime() {
        String date =DateFormat.stampToDate(String.valueOf(System.currentTimeMillis()));
        System.out.println(date);
        StringBuilder  sbl = new StringBuilder(date);
        String min= sbl.substring(14,16);
        System.out.println(min);
        if(min.equals("59") ||min.equals("00") ||min.equals("01")){
             return true;
        }else {
            return false;
        }
    }




}
